package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.CustomerEntity;
import com.ly.heart.domain.TenderEntity;
import com.ly.heart.service.ICustomerService;
import com.ly.heart.service.ITenderService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.BidMapper;
import com.ly.heart.domain.BidEntity;
import com.ly.heart.service.IBidService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 标书管理，审批该标书是否可用Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class BidServiceImpl extends ServiceImpl<BidMapper,BidEntity> implements IBidService
{
    @Resource
    private BidMapper bidMapper;
    @Resource
    private ITenderService tenderService;
    @Resource
    private ICustomerService customerInfoService;

    @Override
    public BidEntity selectBidVoById(String id){
        BidEntity bidEntity = this.getById(id);
        Optional.ofNullable(bidEntity).ifPresent(bid -> {
            TenderEntity tender = tenderService.getById(bid.getTenderId());
            bidEntity.setTender(Optional.ofNullable(tender).orElse(new TenderEntity()));
            CustomerEntity customerEntity = customerInfoService.getById(bid.getCustomerId());
            bidEntity.setCustomer(Optional.ofNullable(customerEntity).orElse(new CustomerEntity()));
        });
        return bidEntity;
    }

    @Override
    public AjaxResult selectBidList(BidEntity bidEntity){
        List<BidEntity> bidList = bidMapper.selectBidEntityList(bidEntity);
        if(!CollectionUtils.isEmpty(bidList)){
            bidList.forEach(bid -> {
                TenderEntity tender = tenderService.getById(bid.getTenderId());
                bid.setTender(Optional.ofNullable(tender).orElse(new TenderEntity()));
                CustomerEntity customerEntity = customerInfoService.getById(bid.getCustomerId());
                bid.setCustomer(Optional.ofNullable(customerEntity).orElse(new CustomerEntity()));
            });
        }
        Page<BidEntity> page = new Page<>(bidEntity.getPage(),bidEntity.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,bidList));
    }

    /**
     * 查询标书管理，审批该标书是否可用
     *
     * @param id 标书管理，审批该标书是否可用主键
     * @return 标书管理，审批该标书是否可用
     */
    @Override
    public BidEntity selectBidEntityById(String id)
    {
        return bidMapper.selectBidEntityById(id);
    }

    /**
     * 查询标书管理，审批该标书是否可用列表
     *
     * @param bidEntity 标书管理，审批该标书是否可用
     * @return 标书管理，审批该标书是否可用
     */
    @Override
    public List<BidEntity> selectBidEntityList(BidEntity bidEntity)
    {
        return bidMapper.selectBidEntityList(bidEntity);
    }

    /**
     * 新增标书管理，审批该标书是否可用
     *
     * @param bidEntity 标书管理，审批该标书是否可用
     * @return 结果
     */
    @Override
    public int insertBidEntity(BidEntity bidEntity)
    {
        bidEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
        bidEntity.setCreateTime(DateUtils.getNowDate());
        return bidMapper.insertBidEntity(bidEntity);
    }

    /**
     * 修改标书管理，审批该标书是否可用
     *
     * @param bidEntity 标书管理，审批该标书是否可用
     * @return 结果
     */
    @Override
    public int updateBidEntity(BidEntity bidEntity)
    {
        bidEntity.setChangeTime(DateUtils.getNowDate());
        return bidMapper.updateBidEntity(bidEntity);
    }

    /**
     * 批量删除标书管理，审批该标书是否可用
     *
     * @param ids 需要删除的标书管理，审批该标书是否可用主键
     * @return 结果
     */
    @Override
    public int deleteBidEntityByIds(String[] ids)
    {
        return bidMapper.deleteBidEntityByIds(ids);
    }

    /**
     * 删除标书管理，审批该标书是否可用信息
     *
     * @param id 标书管理，审批该标书是否可用主键
     * @return 结果
     */
    @Override
    public int deleteBidEntityById(String id)
    {
        return bidMapper.deleteBidEntityById(id);
    }
}
